import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:industry_net/base/common/util.dart';
import 'package:industry_net/pages/me/main/models/usually.dart';

class DragableItemView extends StatelessWidget {
  const DragableItemView({
    Key? key,
    required this.itemSize,
    required this.md,
  }) : super(key: key);
  final double itemSize;
  final UsuallyModel md;
  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: md.onTap,
      child: Container(
        width: itemSize,
        height: itemSize,
        color: ColorsUtil.red,
        child: Text(md.name),
      ),
    );
  }
}

class DragableListView extends StatelessWidget {
  const DragableListView({Key? key, required this.itemSize, required this.list})
      : super(key: key);
  final double itemSize;
  final List<UsuallyModel> list;
  @override
  Widget build(BuildContext context) {
    return Column(
      children: list
          .map((e) => DragableItemView(
                itemSize: itemSize,
                md: e,
              ))
          .toList(),
    );
  }
}

void dragableItem({
  required UsuallyModel md,
  required List<UsuallyModel> expandList,
}) {
  var _right = gap12.obs;
  var _bottom = 100.h.obs;
  var _itemSize = 60.h;
  var _height = 60.h.obs;
  var _isDownExpend = false;
  var _isExpand = false;
  var _item = OverlayEntry(
    maintainState: true,
    builder: (ctx) {
      return Obx(
        () => Positioned(
          right: _right.value,
          bottom: _bottom.value,
          width: _itemSize,
          height: _height.value,
          child: GestureDetector(
            onTap: () {
              _isExpand = !_isExpand;
              if (_isExpand) {
                _height.value = _itemSize * expandList.length;
              } else {
                if (_isDownExpend) {
                  _bottom.value += (_height.value - _itemSize);
                  _isDownExpend = false;
                }

                _height.value = _itemSize;
              }

              if (_bottom.value + _height.value >
                  Get.height - SizeFit.navBarHeight - _itemSize) {
                _bottom.value -= (_height.value - _itemSize);
                _isDownExpend = true;
              }
            },
            child: Draggable(
              feedback: _isExpand
                  ? DragableListView(
                      itemSize: _itemSize,
                      list: expandList,
                    )
                  : DragableItemView(
                      itemSize: _itemSize,
                      md: md,
                    ),
              child: _isExpand
                  ? DragableListView(
                      itemSize: _itemSize,
                      list: expandList,
                    )
                  : DragableItemView(
                      itemSize: _itemSize,
                      md: md,
                    ),
              onDragUpdate: (details) {
                if (_isExpand) {
                  return;
                }
                var _maxX = _right.value;
                var _maxY = _bottom.value;

                _maxX -= details.delta.dx;
                _maxY -= details.delta.dy;
                if (_maxX < gap12) {
                  _maxX = gap12;
                }
                if (_maxX > Get.width - gap12 - _itemSize) {
                  _maxX = Get.width - gap12 - _itemSize;
                }
                if (_maxY <= Get.bottomBarHeight) {
                  _maxY = Get.bottomBarHeight;
                }
                if (_maxY >= Get.height - SizeFit.navBarHeight - _itemSize) {
                  _maxY = Get.height - SizeFit.navBarHeight - _itemSize;
                }
                _right.value = _maxX;
                _bottom.value = _maxY;
              },
              onDragEnd: (details) {
                if (_right.value < gap12) {
                  _right.value = gap12;
                }
                if (_right.value < (Get.width - _itemSize) / 2) {
                  _right.value = gap12;
                } else {
                  _right.value = Get.width - gap12 - _itemSize;
                }
                if (_bottom.value < Get.bottomBarHeight) {
                  _bottom.value = Get.bottomBarHeight;
                }
                if (_bottom.value >=
                    Get.height - SizeFit.navBarHeight - _itemSize) {
                  _bottom.value = Get.height - SizeFit.navBarHeight - _itemSize;
                }
              },
            ),
          ),
        ),
      );
    },
  );
  Overlay.of(Get.context!)?.insert(_item);
}
